const getApiUrl = function(){
	// console.log('envVersion', __wxConfig.envVersion);
	let version = __wxConfig.envVersion;
	let apiUrl = ''
	// 需要时更改
	switch (version)
	{
		case 'develop':
			apiUrl = 'https://yx.tfnhg.com'; //开发版环境域名
			// apiUrl = 'https://yhq.freei.cc',//线下接口
			break;
		case 'trial':
			apiUrl = 'https://yx.tfnhg.com'; //体验版环境域名
			break;
		case 'release':
			apiUrl = 'https://yx.tfnhg.com'; //线上环境域名
			break;
		default:
			apiUrl = 'https://yx.tfnhg.com'; //测试版环境域名
			break;
	}
	return apiUrl
}

const apiUrl = getApiUrl()

const showToast = function(icon,title){
	uni.showToast({
		title: title,
		icon: icon,
		duration: 1000
	})
}

const showModalAll = function(title,content,callback,cancelback){
    uni.showModal({
        title: title,
        content: content,
        success(res) {
            if (res.confirm) {
                callback()
            } else if (res.cancel) {
                cancelback()
            }
        }
    })
}

const showModal = function(title,content){
    uni.showModal({
        title: title,
        content: content,
		showCancel:false,
		success (res) {
			
		}
    })
}

const number = 20

const getAjax = function(url,callback,data){
	uni.showLoading({
		title:'加载中'
	})
	uni.request({
		url:url,
		data:data,
		method:'GET',
		success(res){
			uni.hideLoading()
			callback(res)
		},
		fail(err){
			errData()
			failback(err)
			uni.hideLoading()
			this.showToast('none','请求失败')
		}
	})
}

const postAjax = function(url,callback,data,failback){
	uni.showLoading({
		title:'加载中'
	})
	uni.request({
		url:url,
		data:data,
		method:'POST',
		header:{
			'content-type': 'application/x-www-form-urlencoded'
		},
		success(res){
			callback(res)
			uni.hideLoading()
		},
		fail(err){
			errData()
			failback(err)
			uni.hideLoading()
			this.showToast('none','请求失败')
		}
	})
}

const errData = function(err){
	showToast('none','请求失败,请检查网络')
	return
}

const parsePrice = function(val){
  if (!val) {
    val = 0;
  }
  return val.toFixed(2).split(".");
}


// 判断公众号截取code
const getUrlParam = (name) => {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}

//判断是否为微信
const isWechat = () => {
	return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
}

//正则验证
const validation = function(data,type) {
    let str = ''
    if(type == 'phone')
    {
      str = /^1\d{10}$/
    }
    else if(type == 'idcard')
    {
      str = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    }
    else if(type == 'name')
    {
      str = /^[\u4E00-\u9FA5A-Za-z]+$/
    }
    else if(type == 'number')
    {
      // 验证非负浮点数
      str = /^(\-|\+)?\d+(\.\d+)?$/
    }
    else if(type == 'alphanumeric')
    {
      // 验证由数字和26个英文字母组成的字符串
      str = /^[A-Za-z0-9]+$/
    }
    if (str.test(data)) {
      return true
    }
    else{
      return false
    }
}

const getStatus = function(index){
	let status = 10
	switch (index){
		case 0:
			status = 10
			break;
		case 1:
			status = 0
			break;
		case 2:
			status = 1
			break;
		case 3:
			status = 2
			break;
		case 4:
			status = 3
			break;
	}
	return status
}

const getIntegralStatus = function(index){
	let status = 1
	switch (index){
		case 0:
			status = 1
			break;
		case 1:
			status = 2
			break;
	}
	return status
}

const getOrderStatus = function(status){
	let statusTxt = ''
	switch (status) {
		case '0':
			statusTxt = '待付款'
			break;
		case '1':
			statusTxt = '待发货'
			break;
		case '2':
			statusTxt = '待收货'
			break;
		case '3':
			statusTxt = '交易完成'
			break;
		case '4':
			statusTxt = '交易关闭'
			break;
		case '5':
			statusTxt = '退款'
			break;
		case '6':
			statusTxt = '已删除'
			break;
		case '7':
			statusTxt = '退款成功'
			break;
	}
	return statusTxt
}

const getIntegralOrderStatus = function(status){
	let statusTxt = ''
	switch (status) {
		case '1':
			statusTxt = '待发货'
			break;
		case '2':
			statusTxt = '已发货'
			break;
	}
	return statusTxt
}

const getRefundStatus = function(status){
	let statusTxt = ''
	switch (status) {
		case '0':
			statusTxt = '申请退款'
			break;
		case '1':
			statusTxt = '退款成功'
			break;
		case '2':
			statusTxt = '退款拒绝'
			break;
		case '4':
			statusTxt = '正常订单'
			break;
	}
	return statusTxt
}

const getShopGoodsStatus = function(status){
	let statusTxt = ''
	switch (status) {
		case '0':
			statusTxt = '该商品正在审核中...'
			break;
		case '1':
			statusTxt = '该商品已上架'
			break;
		case '2':
			statusTxt = '该商品已下架'
			break;
		case '3':
			statusTxt = '该商品审核未通过'
			break;
		case '4':
			statusTxt = '该商品已删除'
			break;
		default:
			statusTxt = '该商品不存在'
			break
	}
	return statusTxt
}

const getGoodsStatus = function(status){
	let statusTxt = ''
	switch (status) {
		case '0':
			statusTxt = '正在审核中...'
			break;
		case '1':
			statusTxt = '已上架'
			break;
		case '2':
			statusTxt = '已下架'
			break;
		case '3':
			statusTxt = '审核未通过'
			break;
		case '4':
			statusTxt = '商品已删除'
			break;
	}
	return statusTxt
}

const setTime = function(timestr){
	var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear();
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var D = change(date.getDate());
	var h = change(date.getHours());
	var m = change(date.getMinutes());
	var s = change(date.getSeconds());
	return Y + '-' + M + '-' + D+ ' ' + h + ':' + m + ':' + s;
}

const setAllTime = function(date){
	var Y = date.getFullYear();
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var D = change(date.getDate());
	var h = change(date.getHours());
	var m = change(date.getMinutes());
	var s = change(date.getSeconds());
	return Y + '-' + M + '-' + D+ ' ' + h + ':' + m + ':' + s;
}

const timeFn =  function(start,close) {
    //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
    var dateDiff = close - start;//时间差的毫秒数
    var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
    var leave1=dateDiff%(24*3600*1000)    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000))//计算出小时数
    //计算相差分钟数
    var leave2=leave1%(3600*1000)    //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000))//计算相差分钟数
    //计算相差秒数
    var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000)

    var leave4=leave3%(60*1000)      //计算分钟数后剩余的毫秒数
    var minseconds=Math.round(leave4/1000)
    var time = dayDiff + "-" + hours + "-"+ minutes + "-" + seconds;
    return time;
}

function change(t) {
	if (t < 10) {
		return "0" + t;
	} else {
		return t;
	}
}

const calculate = function(a,b,c){
	return (Number(a)*Number(b)) + Number(c?c:0)
}

export default{
	showToast: showToast,
	getAjax: getAjax,
	postAjax: postAjax,
	errData: errData,
	number: number,
	showModal: showModal,
	showModalAll: showModalAll,
	getUrlParam: getUrlParam,
	isWechat: isWechat,
	parsePrice: parsePrice,
	validation: validation,
	getStatus: getStatus,
	getOrderStatus: getOrderStatus,
	setTime: setTime,
	setAllTime: setAllTime,
	timeFn: timeFn,
	getRefundStatus: getRefundStatus,
	getGoodsStatus: getGoodsStatus,
	getShopGoodsStatus: getShopGoodsStatus,
	apiUrl,
	calculate,
	getIntegralStatus,
	getIntegralOrderStatus,
	getApiUrl
}